from tCube import TCube
from vec2 import Vec2
import math
from eventServer import eventServer
from soundServer import sound

class PlayerCube(TCube):
    
    def __init__(self):
        TCube.__init__(self)
        
        self.setVar("walk", 5)
        self.setVar("fast", 2)
        self.setVar("jumpFast", 1.25)
        self.setVar("jump", 25)
        self.setVar("face", "left")
        self.setVar("base", "player")
        self.setVar("isWalk", False)
        self.setVar("isFast", False)
        self.setVar("carryFlag", False)
        self.setVar("collectFlag", True)
        self.setVar("itemCount", 0)
        self.jumpCount = 0
        self.jumpCountMax = 0.1
        self.jumpingCount    = 0
        self.jumpingCountMax = 1
        self.jumpMult = 1
        self.isJump = False
        self.fallCount = 0
        self.fallCountMax = 0.1
        self.stompThresh = 0.1
        self.fallObject = None
        
        self.setVar("dVel", Vec2())
        
    def jump(self):
        if(self.jumpCount > 0):
            self.setVar("animation","Jump")
            
            self.jumpingCount = self.jumpingCountMax
            self.jumpMult = 1
            if(self.getVar("isFast")):
                self.jumpMult = self.getVar("jumpFast")
            v   = self.getVar("vel")
            v.y = self.getVar("jump") * self.jumpMult
            self.isJump = True
            sound.play("jump")
            
    def stopJump(self):
        if(self.isJump):
            self.setVar("animation","Stand")
            
            self.isJump = False
            v = self.getVar("vel")
            v.y -= self.jumpingCount * self.jumpMult * self.getVar("jump")
            
    def fall(self):
        if(self.fallCount > 0):
            if(not(self.fallObject == None)):
                self.setIgnoreSurface(self.fallObject[0], self.fallObject[1])
    
    def walk(self, Left):
        self.setVar("animation","Walk")
        
        self.setVar("isWalk", True)
        dv = self.getVar("dVel")
        w = self.getVar("walk")
        f = 1
        if(self.getVar("isFast")):
            f = self.getVar("fast")
        if(Left):
            dv.x = -w * f
            self.setVar("face", "left")
        else:
            dv.x = w * f
            self.setVar("face", "right")
            
    def stopWalk(self):
        self.setVar("animation","Stand")
        
        self.setVar("isWalk", False)
        dv = self.getVar("dVel")
        dv.x = 0
        
    def updateWalk(self):
        if(self.getVar("isWalk")):
            if(self.getVar("face") == "left"):
                self.walk(True)
            else:
                self.walk(False)
    
    def goFast(self):
        self.setVar("isFast", True)
        if(self.onLand()):
            self.updateWalk()
    
    def stopGoFast(self):
        self.setVar("isFast", False)
        if(self.onLand()):
            self.updateWalk()
    
    def grabItem(self):
        self.setVar("carryFlag", True)
    
    def useItem(self):
        self.setVar("carryFlag", False)
        eventServer.pull("itemDropped",{"player":self.getVar("player")})
        
    def onLand(self):
        if(self.jumpCount > 0):
            return True
        else:
            return False
   
    def hitLand(self):
        self.updateWalk()
   
    def update(self, cmd, args):
        TCube.update(self, cmd, args)
        
        dt = args["dtime"]
        
        if(self.isJump):
            self.jumpingCount -= dt
            if(self.jumpingCount < 0):
                self.stopJump()
        
        if(self.jumpCount > -10):
            self.jumpCount -= dt
        if(self.fallCount > -10):
            self.fallCount -= dt
        itemCount = self.getVar("itemCount")
        if(itemCount > -10):
            self.setVar("itemCount", itemCount - dt)
            
        if not self.getVar("dead") and self.getVar("pos").y < -20:
            print "player died!"
            eventServer.pull('playerDied',{'player':self.getVar("player")})
        
    def onCollision(self, dist, vec, side, sideType, above, toward, ignoreSurface, object):
        TCube.onCollision(self, dist, vec, side, sideType, above, toward, ignoreSurface, object)
        
        dv = self.getVar("dVel")
        
        objectBase = self.getBase(object)
        if(objectBase == "player"):
            fromPlayer = self.getVar("player")
            toPlayer = object.getVar("player")
            eventServer.pull('playerTouch',{'fromPlayer':fromPlayer,'toPlayer':toPlayer})
        
        if(side == 1):
            if(objectBase == "player"):
                if(not(self.onLand())):
                    fromPlayer = self.getVar("player")
                    toPlayer = object.getVar("player")
                    print "player %d stomped player %d" % (fromPlayer,toPlayer)
                    eventServer.pull('playerStomp',{'fromPlayer':fromPlayer,'toPlayer':toPlayer})
                    sound.play("stomp")
        
        if((sideType == "s") or (sideType == "o" and above and (not ignoreSurface) and toward)):
            if(side == 1):
                if(sideType == "o"):
                    self.fallObject = [object, side]
                    self.fallCount = self.fallCountMax = 0.1
                self.clearIgnoreSurface()

                if(dv.y <= 0):
                    if(self.jumpCount < 0):
                        self.hitLand()
                    self.jumpCount = self.jumpCountMax
            elif(side == 3):
                self.stopJump()
